package com.yjd.job.order;

import com.alibaba.edas.schedulerX.ProcessResult;
import com.alibaba.edas.schedulerX.ScxSimpleJobContext;
import com.alibaba.edas.schedulerX.ScxSimpleJobProcessor;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.StagesStatusEnum;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.util.*;
import org.apache.log4j.Logger;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单失效检查更新（超过24小时未只支付订单）
 * Created by yjd-wap on 2017/11/21.
 */
public class OrderOverdueCheckJob implements ScxSimpleJobProcessor {
    Logger logger = Logger.getLogger(OrderOverdueCheckJob.class);

    @Resource
    private IOrderService orderService;

    @Override
    public ProcessResult process(ScxSimpleJobContext scxSimpleJobContext) {
        Long currentTime = FrameUtil.getTime();
        try {
            // 每次更新100条记录
            List<OrderModel> orders = orderService.getModelList(
                    FrameUtil.newHashMap("status", StagesStatusEnum.APPLY_SUCCESS.getCode(), "invalidTime", currentTime - 86400),
                    0, 100, DbWREnums.READ);

            for(OrderModel om: orders){
                orderService.updateInvalidOrderStatus( FrameUtil.newHashMap("order_id", om.getOrder_id()) );
            }

            logger.info("订单有效期检查更新完成，时间：" + currentTime + "，更新记录数："+ orders.size());
        } catch (Exception e) {
            logger.error("更新失效订单失败", e);
        }
        return new ProcessResult(true);
    }
}
